package com.googlecode.stemwijzer.server.listeners;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.googlecode.stemwijzer.server.modules.StemwijzerModule;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

/**
 * Insert point for Jersey to start the Guice configuration (of the Rest-ful service implementation).
 * 
 * @author roel.hodzelmans
 */
public class RestGuiceServletConfigListener extends GuiceServletContextListener {

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.google.inject.servlet.GuiceServletContextListener#getInjector()
	 */
	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new StemwijzerModule(), new ServletModule() {

			/**
			 * {@inheritDoc}
			 * 
			 * @see com.google.inject.servlet.ServletModule#configureServlets()
			 */
			@Override
			protected void configureServlets() {
				final Map<String, String> params = new HashMap<String, String>();
				params.put(PackagesResourceConfig.PROPERTY_PACKAGES,
						"com.googlecode.stemwijzer.server.rest;com.sun.jersey.json.impl.provider.entity");

				// Add the RESTful jersey implementation servlet.
				this.serve("/*").with(GuiceContainer.class, params);
			}
		});
	}
}
